package com.xyh.transaction.controller;

import com.xyh.transaction.entity.Address;
import com.xyh.transaction.response.ResponseResult;
import com.xyh.transaction.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.math3.analysis.function.Add;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "地址管理")
@RestController
@RequestMapping("address")
public class AddressController {


    @Autowired
    private AddressService addressService;


    @ApiOperation(value = "新增地址接口",notes = "新增当前用户收货地址")
    @PostMapping("add")
    public ResponseResult<Void> add(@RequestBody Address address){
        addressService.add(address);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "用户地址列表", notes = "返回当前用户地址列表")
    @GetMapping("list")
    public ResponseResult<List<Address>> list() {
        List<Address> addresses = addressService.getList();
        return ResponseResult.success("查询成功!", addresses);
    }

    @ApiOperation(value = "删除地址", notes = "根据id删除地址")
    @GetMapping("delete/{id}")
    public ResponseResult<Void> delete(@PathVariable Integer id) {
        addressService.deleteAddress(id);
        return ResponseResult.success("操作成功!", null);
    }

    @ApiOperation(value = "默认地址set", notes = "设置默认地址")
    @GetMapping("/default/{id}")
    public ResponseResult<Void> defaultAddress(@PathVariable Integer id) {
        addressService.setDefaultAddress(id);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "修改地址", notes = "根据id修改已经存在地址信息")
    @PutMapping("/update")
    public ResponseResult<Void> editAddress(@RequestBody Address address) {
        addressService.editAddress(address);
        return ResponseResult.success("操作成功！", null);
    }

}
